/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rbh.risk.dao.impl;

import com.rbh.risk.dao.OpduserDao;
import com.rbh.risk.entity.Opduser;
import com.rbh.risk.support.SessionFactorySupport;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

@Repository("opduserDao")
public class OpduserDaoImpl extends SessionFactorySupport implements OpduserDao {

    @Override
    public Opduser getByUsername(String userName) throws DataAccessException {
        Opduser result = null;
        try {
            result = (Opduser) super.getSessionFactory().getCurrentSession().get(Opduser.class, userName);
        } catch (Exception ex) {
            throw ex;
        }
        return result;
    }

    @Override
    public List<Opduser> getByWords(String words) throws DataAccessException {
        List<Opduser> results = null;
        try {
            Criteria criteria = super.getSessionFactory().getCurrentSession().createCriteria(Opduser.class);
            criteria.add(Restrictions.disjunction()
                    .add(Restrictions.like("loginname", words, MatchMode.ANYWHERE))
                    .add(Restrictions.like("name", words, MatchMode.START)));
            criteria.addOrder(Order.asc("loginname"));
            criteria.addOrder(Order.asc("name"));
            criteria.setMaxResults(100);
            results = criteria.list();
        } catch (Exception ex) {
            throw ex;
        }
        return results;
    }
}
